package com.kfgj.hkp.common.utils;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;

import java.text.SimpleDateFormat;
import java.util.Date;


public class StringToDateConverter implements Converter<String,Date>{

    Logger logger = LoggerFactory.getLogger (getClass ());

    private String pattern;
    public StringToDateConverter(String pattern){
        this.pattern=pattern;
        logger.debug("初始化**********"+pattern);
    }

    @Override
    public Date convert(String str){
        try{
            logger.debug ("*************convert 被调用了*************");
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(pattern);
            simpleDateFormat.setLenient(false);
            Date date= simpleDateFormat.parse(str);
            logger.debug("格式化之前的str"+str+"格式化之后的date"+date);
            return date;
        }catch (Exception e) {
            return null;
        }
    }
}
